﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Inspired.Euler
{
    public static class Problem049
    {
        /// <summary>
        /// Find arithmetic sequences, made of prime terms, whose four digits are permutations of each other.
        /// </summary>
        [EulerProblem(49, Title = "Find arithmetic sequences, made of prime terms, whose four digits are permutations of each other.")]
        public static long Solve()
        {
            for (int i = 1; i <= 9999; i++)
            {
                Console.Title = i.ToString();
                if (i == 1487)
                    continue;

                for (int plus = 1; plus < 3333; plus++)
                {
                    long n1 = i;
                    long n2 = n1 + plus;
                    long n3 = n2 + plus;
                    if (n1.IsPrime() && n2.IsPrime() && n3.IsPrime() &&
                        n1.AsEnumerable().Intersect(n2.AsEnumerable().Intersect(n3.AsEnumerable())).Count() == 4)
                    {
                        return
                            n1.AsEnumerable().Concat(
                            n2.AsEnumerable().Concat(
                            n3.AsEnumerable())).ToInt64();
                    }
                }
            }
            return 0;
        }
    }
}
